#include "Animation.h"
#include "GameTime.h"

Animation::Animation(void)
{
	_frameInc = 1;
	_oldTime = 0;
	_done = false;
	_reverse = false;
	_pause = false;
}

Animation::Animation(const std::string& id, int startFrame, int startupFrame, int maxFrames, int row, int frameRate, bool repeat, bool reverseWhenRepeat, bool oscillate, int timesRepeat)
{
	_startupFrame = startupFrame;
	_id = id;
	_startFrame = startFrame;
	_maxFrames = maxFrames;
	_row = row;
	_frameRate = frameRate;
	_oscillate = oscillate;
	_repeat = repeat;

	_currentFrame = _startupFrame;
	_frameInc = 1;
	_oldTime = 0;
	_done = false;
	_reverse = false;
	_pause = false;
	_reverseWhenRepeat = reverseWhenRepeat;
	if (timesRepeat != 0)
	{
		_timesRepeat = timesRepeat;
		_maxTimesRepeat = timesRepeat;
		_fixedRepeat = true;
	}
	else
	{
		_timesRepeat = 0;
		_maxTimesRepeat = 0;
		_fixedRepeat = false;
	}
}

Animation::~Animation(void){}

void Animation::update()
{
	if (_oldTime + static_cast<long>(_frameRate) > static_cast<long>(GameTime::getSingleton()->getTotalTimeAsMilliseconds()))
	{
		return;
	}

	_oldTime = static_cast<long>(GameTime::getSingleton()->getTotalTimeAsMilliseconds());

	if (!_pause)
	{
		if (!_done)
		{
			if (!_reverse)
				_currentFrame += _frameInc;
			else
				_currentFrame -= _frameInc;
		}
	}

	if(_oscillate) 
	{
        if(_frameInc > 0) 
		{
            if(_currentFrame >= _maxFrames) 
			{
                _frameInc = -_frameInc;
            }
        }
		else
		{
            if(_currentFrame <= 0) 
			{
                _frameInc = -_frameInc;
            }
        }
    }
	else
	{
        if(_currentFrame > _maxFrames && !_reverse) 
		{
			if (_repeat && !_fixedRepeat)
			{
				_currentFrame = _startFrame;
			}
			else if (_fixedRepeat)
			{
				if (_timesRepeat <= 0)
					_done = true;
				_timesRepeat--;
				if (_reverseWhenRepeat)
				{
					_currentFrame = _maxFrames;
					_reverse = !_reverse;
				}
				else
				{
					_currentFrame = _startFrame;
				}
			}
			else
			{
				_done = true;
				_currentFrame = _maxFrames;
			}
        }
		else if (_currentFrame < _startFrame && _reverse)
		{
			if (_repeat && !_fixedRepeat)
			{
				_currentFrame = _maxFrames;
			}
			else if (_fixedRepeat)
			{
				if (_timesRepeat <= 0)
				{
					_done = true;
				}
				_timesRepeat--;
				if (_reverseWhenRepeat)
				{
					_currentFrame = _startFrame;
					_reverse = !_reverse;
				}
				else
				{
					_currentFrame = _maxFrames;
				}
			}
			else
			{
				_done = true;
				_currentFrame = _startFrame;
			}
		}
    }
}

void Animation::setCurrentFrame(int frame)
{
	if (frame < _startFrame || frame >= _maxFrames)
	{
		return;
	}

	_currentFrame = frame;
}

void Animation::setAnimationData(const std::string& id, int startFrame, int maxFrames, int row, int frameRate, bool repeat, bool oscillate)
{
	_id = id;
	_startFrame = startFrame;
	_maxFrames = maxFrames;
	_row = row;
	_frameRate = frameRate;
	_oscillate = oscillate;
	_repeat = repeat;
	_currentFrame = _startFrame;
}

void Animation::activate()
{
	_oldTime = static_cast<long>(GameTime::getSingleton()->getTotalTimeAsMilliseconds());
}